package com.neusoft.nep.controller;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class PubilcSupervisorView2Controller {

    @FXML
    private RadioButton levelOneRadioButton;
    @FXML
    private RadioButton levelTwoRadioButton;
    @FXML
    private RadioButton levelThreeRadioButton;
    @FXML
    private RadioButton levelFourRadioButton;
    @FXML
    private RadioButton levelFiveRadioButton;
    @FXML
    private RadioButton levelSixRadioButton;
    @FXML
    private TextField feedbackTextField;

    private ToggleGroup aqiToggleGroup;
    private Map<String, Object> feedbackData = new HashMap<>();

    @FXML
    public void initialize() {
        aqiToggleGroup = new ToggleGroup();
        levelOneRadioButton.setToggleGroup(aqiToggleGroup);
        levelTwoRadioButton.setToggleGroup(aqiToggleGroup);
        levelThreeRadioButton.setToggleGroup(aqiToggleGroup);
        levelFourRadioButton.setToggleGroup(aqiToggleGroup);
        levelFiveRadioButton.setToggleGroup(aqiToggleGroup);
        levelSixRadioButton.setToggleGroup(aqiToggleGroup);
    }

    public void setFeedbackData(Map<String, Object> data) {
        this.feedbackData.putAll(data);
    }

    @FXML
    public void handleSubmitButtonAction() {
        String selectedLevel = null;
        if (levelOneRadioButton.isSelected()) {
            selectedLevel = "一";
        } else if (levelTwoRadioButton.isSelected()) {
            selectedLevel = "二";
        } else if (levelThreeRadioButton.isSelected()) {
            selectedLevel = "三";
        } else if (levelFourRadioButton.isSelected()) {
            selectedLevel = "四";
        } else if (levelFiveRadioButton.isSelected()) {
            selectedLevel = "五";
        } else if (levelSixRadioButton.isSelected()) {
            selectedLevel = "六";
        }

        String feedbackInfo = feedbackTextField.getText();

        if (selectedLevel != null && !feedbackInfo.isEmpty()) {
            feedbackData.put("空气质量等级", selectedLevel);
            feedbackData.put("反馈信息", feedbackInfo);
            feedbackData.put("时间", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

            saveFeedbackData();

            System.out.println("Feedback submitted successfully.");
        } else {
            System.out.println("请选择空气质量等级并填写反馈信息。");
        }
    }

    // 在文件中储存反馈数据（包括前一个界面的）
    private void saveFeedbackData() {
        String fileName = "FeedbackData.txt";

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true))) {
            writer.write("--------------------------------------------------");
            writer.newLine();
            writer.write("时间: " + feedbackData.get("时间"));
            writer.newLine();
            writer.write("省份: " + feedbackData.get("省份"));
            writer.newLine();
            writer.write("城市: " + feedbackData.get("城市"));
            writer.newLine();
            writer.write("具体地址: " + feedbackData.get("具体地址")); // 添加具体地址到文件
            writer.newLine();
            writer.write("手机号: " + feedbackData.get("手机号")); // 将手机号写入文件
            writer.newLine();
            writer.write("空气质量等级: " + feedbackData.get("空气质量等级"));
            writer.newLine();
            writer.write("反馈信息: " + feedbackData.get("反馈信息"));
            writer.newLine();
            writer.write("--------------------------------------------------");
            writer.newLine();
            System.out.println("Data saved to " + fileName);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}